package com.thingple.app.io;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.thingple.json.JsonConvertor;
import com.thingple.rest.RestClient;
import com.thingple.shared_context.SharedContext;
import com.thingple.version.uuid.GlobalKeyHandler;
import com.thingple.version.uuid.UUID;

import java.util.HashMap;
import java.util.Map;

public class LicenceActivity extends AppCompatActivity {

    private String defaultHost = "http://baominstatic.thingplecloud.com/css/m";

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_licence);
    }

    public void licenceBtnClick(View view) {
        String licenceUri = null;
        EditText editText = (EditText) findViewById(R.id.txt_label);
        String code = editText.getText().toString();
        if (code.trim().equals("")) {
            return;
        }
        EditText editTextLicence = (EditText) findViewById(R.id.txt_licenceserver);
        String specialLicenceServer = editTextLicence.getText().toString();
        if (!specialLicenceServer.trim().equals("")) {
            licenceUri = specialLicenceServer;
        }
        if (licenceUri == null) {
            SharedContext sharedContext = SharedContext.shareInstance();
            licenceUri = sharedContext.get("licence.uri");
            if (licenceUri == null) {
                licenceUri = defaultHost;
            }
        }

        String uri = licenceUri + code;

        installLicence(uri);
    }

    public void installLicence(final String uri) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, String> params = new HashMap<>();
                params.put("t", System.currentTimeMillis() + "");
                String text = RestClient.newInstance().get(uri, params);
                final UUID uuid = JsonConvertor.convert2Object(text, UUID.class);

                if (uuid != null && uuid.uuid != null && uuid.uri != null && uuid.vendor != null) {
                    GlobalKeyHandler globalKeyHandler = new GlobalKeyHandler();
                    globalKeyHandler.save(uuid);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            App app = App.instance;
                            Toast.makeText(app, "restart", Toast.LENGTH_SHORT);
                            new VersionControllHandler(app).update();
                            finish();
                        }
                    });
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            App.instance.alert("Invalid code, please try again");
                        }
                    });
                }
            }

        }).start();
    }
}
